package com.zhiqiang.gameserver.netty;

import com.zhiqiang.gameserver.constant.R;
import com.zhiqiang.gameserver.entity.ZQGameServerEntity;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

@ChannelHandler.Sharable
public class NettyGameServerEncoder extends MessageToByteEncoder<ZQGameServerEntity> {

    public NettyGameServerEncoder(){}
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, ZQGameServerEntity msg, ByteBuf out) throws Exception {
        int bodyLength = msg.getData().length;
        out.ensureWritable(msg.getLen());
        out.writeInt(msg.getLen());
        out.writeInt(msg.getMsgType());
        out.writeInt(msg.getUid());
        out.writeInt(msg.getSeq());
        out.writeInt(msg.getResult());
        out.writeInt(msg.getV6());
        out.writeInt(msg.getV7());
        out.writeInt(msg.getV8());
        out.writeInt(msg.getV9());
        out.writeInt(msg.getV10());
        out.writeInt(msg.getV11());
        out.writeInt(msg.getV12());
        if (bodyLength == msg.getLen()- R.EncoderAndDecoder.HEADER_LENGTH && msg.getData().length > 0) {
            out.writeBytes(msg.getData());
        }

    }


}
